package com.comdo.newup_loan.module.notice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.adapter.NoticeListAdapter;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.NoticeBean;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.MyItemDecoration;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/3.
 */

public class NoticeListActivity extends BaseActivity<NoticeListPersenter> implements NoticeListContact.View {
    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private NoticeListAdapter mAdapter;
    private ArrayList<NoticeBean> listData=new ArrayList<NoticeBean>();
    private int page =1;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_notice_list;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(NoticeListActivity.this,"企业公告",true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(NoticeListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new MyItemDecoration());
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                mPresenter.getNoticeList(page);
            }

            @Override
            public void onLoadMore() {
                page++;
                mPresenter.getNoticeList(page);
            }
        });

        listData = new ArrayList<NoticeBean>();
        mAdapter = new NoticeListAdapter(listData,NoticeListActivity.this);
        mAdapter.setOnItemClickListener(new NoticeListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                toDetailActivity(listData.get(position).getId());

            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.refresh();
    }

    @Override
    public NoticeListPersenter getmPresenter() {
        return new NoticeListPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
        mRecyclerView.refreshComplete();
    }

    @Override
    public void getNoticeList(List<NoticeBean> datas) {
        if(page==1)
            listData.clear();
        listData.addAll(datas);
        mAdapter.notifyDataSetChanged();
        mRecyclerView.refreshComplete();
        //if(datas.size()< Config.pageSize)
          //  mRecyclerView.setNoMore(true);
    }
    private void toDetailActivity(int id){
        Intent intent=new Intent(NoticeListActivity.this,NoticeDetailActivity.class);
        intent.putExtra("id",id);
        startActivity(intent);
    }
}
